package com.cocolove2.library_cocodialog.dialogs;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cocolove2.library_cocodialog.CornerUtils;
import com.cocolove2.library_cocodialog.animations.BaseAnimatorEffects;
import com.cocolove2.library_cocodialog.dialogs.base.CocoPopBase;
import com.cocolove2.library_cocodialog.entity.DialogMenuItem;
import com.cocolove2.library_cocodialog.listener.AnimListenerAdapter;

import java.util.ArrayList;

/**
 * popuwindow形式的列表对话框
 * Created by liubo on 8/12/16.
 */
@SuppressWarnings("unused")
public class NormalListCocoPop extends CocoPopBase<NormalListCocoPop> {
    /*ListView*/
    protected ListView mLv;
    /* divider color(ListView divider颜色)*/
    protected int mDividerColor = Color.LTGRAY;
    /* divider height(ListView divider高度)*/
    protected float mDividerHeight = 0.8f;
    /* item press color(ListView item按住颜色)*/
    protected int mItemPressColor = Color.parseColor("#ffcccccc");
    /* item textcolor(ListView item文字颜色)*/
    protected int mItemTextColor = Color.parseColor("#ffffff");
    /* item textsize(ListView item文字大小)*/
    protected float mItemTextSize = 15f;
    /* item extra padding(ListView item额外padding)*/
    protected int mItemExtraLeft;
    protected int mItemExtraTop;
    protected int mItemExtraRight;
    protected int mItemExtraBottom;

    private int mLvWidth, mLvHeight;

    /* adapter(自定义适配器)*/
    protected BaseAdapter mAdapter;
    /* operation items(操作items)*/
    protected ArrayList<DialogMenuItem> mContents = new ArrayList<>();

    private OnClickListener mOnClickListener;

    private int mSelectPosition = -1;
    /*listview item 的动画*/
    protected LayoutAnimationController mLac;

    protected final AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mSelectPosition = position;
            if (!isHasExitAnim()) {//无退出动画
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(NormalListCocoPop.this, mSelectPosition);
                }
                NormalListCocoPop.this.realDismiss();
            } else {//有退出动画
                NormalListCocoPop.this.dismiss();
            }
        }
    };

    public NormalListCocoPop(Context context) {
        super(context);
        init();
    }

    public NormalListCocoPop(Context context, int style) {
        super(context, style);
        init();
    }

    private void init() {
        setDialogBgColor(Color.parseColor("#BB000000"));
        setCornerRadius(5);
        setTriangleWidthAndHeight(24, 12);
        setShowTriangle(true);
        setOutAnimListener(new AnimListenerAdapter() {
            @Override
            public void onAnimEnd() {
                if (mOnClickListener != null && mSelectPosition > -1)
                    mOnClickListener.onClick(NormalListCocoPop.this, mSelectPosition);
            }
        });
    }

    @Override
    protected View onCreatePopView(Context context, ViewGroup parent) {
        /** listview */
        mLv = new ListView(mContext);
        mLv.setLayoutParams(new LinearLayout.LayoutParams(mLvWidth <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : dp2px(mLvWidth),
                mLvHeight <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : dp2px(mLvHeight)));
        mLv.setCacheColorHint(Color.TRANSPARENT);
        mLv.setFadingEdgeLength(0);
        mLv.setVerticalScrollBarEnabled(false);
        mLv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mLv.setOverScrollMode(View.OVER_SCROLL_NEVER);

        return mLv;
    }


    @Override
    protected void setupUiView() {
        super.setupUiView();
        /** listview */
        mLv.setDivider(new ColorDrawable(mDividerColor));
        mLv.setDividerHeight(dp2px(mDividerHeight));
        mLv.setBackground(CornerUtils.cornerDrawable(mDialogBgColor, dp2px(mCornerRadius)));
        if (mAdapter == null) {
            mAdapter = new ListDialogAdapter();
        }
        mLv.setAdapter(mAdapter);
        mLv.setOnItemClickListener(mOnItemClickListener);
        mLv.setLayoutAnimation(mLac);
    }

    public NormalListCocoPop setItems(@NonNull ArrayList<DialogMenuItem> items, OnClickListener listener) {
        mContents.clear();
        mContents.addAll(items);
        mOnClickListener = listener;
        return this;
    }

    public NormalListCocoPop setItems(@NonNull ArrayList<DialogMenuItem> items, BaseAdapter adapter, OnClickListener listener) {
        mContents.clear();
        mContents.addAll(items);
        mAdapter = adapter;
        mOnClickListener = listener;
        return this;
    }


    public NormalListCocoPop setItems(@NonNull String[] items, OnClickListener listener) {
        mContents.clear();
        for (String item : items) {
            DialogMenuItem customBaseItem = new DialogMenuItem(item, 0);
            mContents.add(customBaseItem);
        }
        mOnClickListener = listener;
        return this;
    }
    /**
     * 设置pop的宽度和高度(小于0时,使用wrap_content)
     */
    public NormalListCocoPop setLvWidthAndHeight(int width, int height) {
        mLvWidth = width;
        mLvHeight = height;
        return this;
    }
    /**
     * 设置item间的分割线的颜色
     */
    public NormalListCocoPop setDividerColor(int dividerColor) {
        mDividerColor = dividerColor;
        return this;
    }

    /**
     * 设置item间的分割线的高度
     */
    public NormalListCocoPop setDividerHeight(float dividerHeight) {
        mDividerHeight = dividerHeight;
        return this;
    }

    /**
     * 设置item点击颜色
     */
    public NormalListCocoPop setItemPressColor(int itemPressColor) {
        mItemPressColor = itemPressColor;
        return this;
    }

    /**
     * 设置item文本颜色
     */
    public NormalListCocoPop setItemTextColor(int itemTextColor) {
        mItemTextColor = itemTextColor;
        return this;
    }

    /**
     * 设置item文本大小(dp)
     */
    public NormalListCocoPop setItemTextSize(float itemTextSize) {
        mItemTextSize = itemTextSize;
        return this;
    }

    /**
     * 设置item额外的内间距
     */
    public NormalListCocoPop setItemExtraPadding(int left, int top, int right, int bottom) {
        mItemExtraLeft = left;
        mItemExtraTop = top;
        mItemExtraRight = right;
        mItemExtraBottom = bottom;
        return this;
    }


    class ListDialogAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mContents.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressWarnings("deprecation")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final DialogMenuItem item = mContents.get(position);

            LinearLayout llItem = new LinearLayout(mContext);
            llItem.setOrientation(LinearLayout.HORIZONTAL);
            llItem.setGravity(Gravity.CENTER_VERTICAL);

            ImageView ivItem = new ImageView(mContext);
            ivItem.setPadding(0, 0, dp2px(15), 0);
            llItem.addView(ivItem);

            TextView tvItem = new TextView(mContext);
            tvItem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            tvItem.setSingleLine(true);
            tvItem.setTextColor(mItemTextColor);
            tvItem.setTextSize(TypedValue.COMPLEX_UNIT_SP, mItemTextSize);

            llItem.addView(tvItem);
            float radius = dp2px(mCornerRadius);
            llItem.setBackground(CornerUtils.listItemSelector(radius, Color.TRANSPARENT, mItemPressColor,
                    mContents.size(), position));

            int left = item.mResId == 0 ? dp2px(18) : dp2px(16);
            int top = dp2px(10);
            int right = 0;
            int bottom = dp2px(10);
            llItem.setPadding(left + mItemExtraLeft, top + mItemExtraTop, right + mItemExtraRight, bottom + mItemExtraBottom);

            ivItem.setImageResource(item.mResId);
            tvItem.setText(item.mOperName);
            ivItem.setVisibility(item.mResId == 0 ? View.GONE : View.VISIBLE);

            return llItem;
        }
    }
}
